﻿Imports SlimDX.Direct3D9

Public Interface IShader
    Enum DefaultTextures As Short
        Additive = 0
        Multiplicative = 1
        SignedAdditive = 2
        Vector = 3
    End Enum

    Enum DetailFunctionEnum As Short
        Multiply2X = 0
        Multiply = 1
        Add2X = 2
    End Enum

    Function BeginShader(ByVal tLightmap As Texture) As Integer

    Sub BeginPass(ByVal pass As Integer)

    Sub EndPass()

    Sub EndShader()
End Interface
Public Module Shader
    Public Function Animate(ByVal tFunction As Short, ByVal period As Single, ByVal scale As Single) As Single
        Select Case tFunction
            Case 0
                Return FunctionGenerator.One()
            Case 1
                Return FunctionGenerator.Zero()
            Case 2, 3
                Return FunctionGenerator.Cosine(period, scale)
            Case 4, 5
                Return FunctionGenerator.Triangle(period, scale)
            Case 6, 7
                Return FunctionGenerator.Slide(period, scale)
            Case 8
                Return FunctionGenerator.Noise(scale)
            Case 9
                Return FunctionGenerator.Jitter(scale)
            Case 10
                Return FunctionGenerator.Wander(period, scale)
            Case 11
                Return FunctionGenerator.Spark(scale)
            Case Else
                Return 0.0F
        End Select
    End Function 'AnimationFunction 
End Module
Public NotInheritable Class FunctionGenerator
    Private Shared sparkValue As Single = 0.0F
    Private Shared sparkSeconds As Double = -1.0
    Private Shared jitterValue As Single = 0.0F
    Private Shared jitterSeconds As Double = -1.0
    Private Shared r As New Random
    Public Shared Function One() As Single
        Return 1.0F
    End Function 'One
    Public Shared Function Zero() As Single
        Return 0.0F
    End Function 'Zero
    Public Shared Function Cosine(ByVal period As Double, ByVal scale As Single) As Single
        Return System.Convert.ToSingle(Math.Cos(Seconds / period * Math.PI)) * scale
    End Function 'Cosine
    Public Shared Function Triangle(ByVal period As Double, ByVal scale As Single) As Single
        Dim x As Double = Seconds / period
        If x Mod 2.0 < 1.0 Then
            Return System.Convert.ToSingle(x Mod 1.0) * scale
        Else
            Return (1.0F - System.Convert.ToSingle(x Mod 1.0)) * scale
        End If
    End Function 'Triangle
    Public Shared Function Slide(ByVal period As Double, ByVal scale As Single) As Single
        Return System.Convert.ToSingle((Seconds / period) Mod 1.0) * scale
    End Function 'Slide
    Public Shared Function Noise(ByVal scale As Single) As Single
        Return System.Convert.ToSingle(r.Next(60)) / 60.0F * scale
    End Function 'Noise
    Public Shared Function Jitter(ByVal scale As Single) As Single
        If jitterSeconds - Seconds < 0.0 Then
            jitterSeconds = Seconds + System.Convert.ToDouble(r.Next(10)) / 10.0
            Return jitterValue = System.Convert.ToSingle(r.Next(4)) / 4.0F * scale 'ToDo: Unsupported feature: assignment within expression.
        Else
            Return jitterValue
        End If
    End Function 'Jitter
    Public Shared Function Wander(ByVal period As Double, ByVal scale As Single) As Single
        Return System.Convert.ToSingle(Math.Cos(Seconds / period * Math.PI) + Math.Sin(Seconds / period * Math.PI)) * (scale / 1.414214F)
    End Function 'Wander
    Public Shared Function Spark(ByVal scale As Single) As Single
        If sparkSeconds - Seconds < 0.0 Then
            sparkSeconds = Seconds + System.Convert.ToDouble(r.Next(10)) / 20.0
            Return sparkValue = System.Convert.ToSingle(r.Next(1)) * scale 'ToDo: Unsupported feature: assignment within expression.
        Else
            Return sparkValue
        End If
    End Function 'Spark 
    Private Shared ReadOnly Property Seconds() As Double
        Get
            Return System.Convert.ToDouble(Environment.TickCount) / 1000.0
        End Get
    End Property
End Class 'FunctionGenerator
